package com.ucomm.talent.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.detail.RecommendJobDetails;
import com.ucomm.talent.adapter.RecommendJobsListAdapter;
import com.ucomm.talent.common.DebugUtil;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.model.RecommendedJobsInfo;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.MainHeaderBar;
import me.maxwin.view.IXListViewLoadMore;
import me.maxwin.view.IXListViewRefreshListener;
import me.maxwin.view.XListView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 职位推荐界面
 * Created with IntelliJ IDEA.
 * User: Vita Hao
 * Date: 13-5-29
 * Time: 下午6:06
 * To change this template use File | Settings | File Templates.
 */
public class RecommendJobActivity extends BaseActivity implements  IHeaderBarClickListener{
    private XListView listView;
    private ArrayList<RecommendedJobsInfo> data;
    private MainHeaderBar mainHeaderBar;
    private SearchManager searchManager;
    private RecommendJobsListAdapter adapter;
    private String url = null;
    private CustomProgressDialog customProgressDialog;

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            DebugUtil.Log(RecommendJobActivity.this,"---------- " + customProgressDialog.toString());
            switch (what){
                case AppConstants.MsgStatus.MSG_ERROR:
                    if (customProgressDialog != null && customProgressDialog.isShowing())
                        customProgressDialog.dismiss();
                    ((AppException)msg.obj).makeToast(RecommendJobActivity.this);
                    break;
                case AppConstants.MsgStatus.MSG_SUCCUSS:
                    if (customProgressDialog != null && customProgressDialog.isShowing())
                        customProgressDialog.dismiss();
                    data.addAll((ArrayList<RecommendedJobsInfo>)msg.obj);
                    if(adapter==null){
                    	adapter = new RecommendJobsListAdapter(RecommendJobActivity.this,data);
                        listView.setAdapter(adapter);
                    }else{
                    	adapter.notifyDataSetChanged();
                    }
//                    adapter = new RecommendJobsListAdapter(RecommendJobActivity.this, (ArrayList<RecommendedJobsInfo>)msg.obj);
//                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new ListItemListener());
                    //上拉加载更多
                    listView.setPullLoadEnable(new IXListViewLoadMore() {
                        @Override
                        public void onLoadMore() {
                        	if(AppConfig.CURR_PAGE !=0){
                        		int i = AppConfig.CURR_PAGE;
                        		if(i<AppConfig.PAGE_COUNT){
                        			AppConfig.CURR_PAGE += 1;
                        			String pageUrl = url+"&page="+AppConfig.CURR_PAGE;
                                	searchManager.searchDataFromNetwrok(pageUrl);
                        		}else{
                        			listView.disablePullLoad();
                        		}
                        	}
                        }
                    });
                    //下拉刷新
                    listView.setPullRefreshEnable(new IXListViewRefreshListener() {
                        @Override
                        public void onRefresh() {
                        	searchManager.searchDataFromNetwrok(url);
                        	listView.stopRefresh(AppConfig.formatter.format(new Date(System.currentTimeMillis())));
                        }
                    });
                    break;
                case AppConstants.MsgStatus.MSG_LOADING:
                    if (customProgressDialog != null && !customProgressDialog.isShowing())
                        customProgressDialog.show();
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recommend_job_layout);
        initView();
        searchManager = new SearchManager(this,mHandler);
        Intent i = getIntent();
        String otherUrl = i.getStringExtra("url");
	    if (otherUrl!=null){
	    	url = otherUrl;
	    }else{
	    	url = AppConstants.RECOMMENDED_JOBS;
	    }
        searchManager.searchDataFromNetwrok(url);
        data = new ArrayList<RecommendedJobsInfo>();
        
    }

    private void initView(){
        mainHeaderBar = (MainHeaderBar) findViewById(R.id.main_header_bar);
        mainHeaderBar.setBarClickListener(this);
        customProgressDialog = new CustomProgressDialog(this);
        listView = (XListView) findViewById(R.id.custom_list);
        listView.setmHeaderViewBackground(R.color.custom_list_item_bg_color);
        listView.setmFooterViewBackground(R.color.custom_list_item_bg_color);

    }

    /**
     * 监听返回
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean flag = true;
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.i("vita","-----onKeyDown");
           // overridePendingTransition(R.anim.slide_left_out, R.anim.slide_left_in);
            finish();
        }
        return flag;
    }
	//选中项监听
	class ListItemListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			ArrayList<RecommendedJobsInfo> mData = adapter.getmData();
			RecommendedJobsInfo info = mData.get(arg2-1);
			Intent i = new Intent();
			i.putExtra("info", info);
			i.setClass(RecommendJobActivity.this, RecommendJobDetails.class);
			startActivity(i);
		}
		
	}

    @Override
    public void onBackButtOnClick(View view) {
        finish();
    }

    @Override
    public void onExitButtonOnClick(View view) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}